package com.example.backend.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * Redis缓存配置
 * 提供分布式缓存支持，提升系统性能
 */
@Configuration
@EnableCaching
@ConditionalOnProperty(name = "spring.cache.type", havingValue = "redis", matchIfMissing = false)
public class RedisCacheConfig {

    /**
     * Redis缓存管理器
     */
    @Bean
    public CacheManager redisCacheManager(RedisConnectionFactory connectionFactory) {
        RedisCacheConfiguration defaultConfig = RedisCacheConfiguration.defaultCacheConfig()
                .entryTtl(Duration.ofMinutes(30)) // 默认30分钟过期
                .serializeKeysWith(RedisSerializationContext.SerializationPair
                        .fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair
                        .fromSerializer(new GenericJackson2JsonRedisSerializer()))
                .disableCachingNullValues();

        // 配置不同缓存区域的过期时间
        Map<String, RedisCacheConfiguration> cacheConfigurations = new HashMap<>();
        
        // 工单相关缓存
        cacheConfigurations.put("tickets", defaultConfig.entryTtl(Duration.ofMinutes(15)));
        cacheConfigurations.put("tickets_list", defaultConfig.entryTtl(Duration.ofMinutes(10)));
        cacheConfigurations.put("tickets_search", defaultConfig.entryTtl(Duration.ofMinutes(5)));
        cacheConfigurations.put("tickets_stats", defaultConfig.entryTtl(Duration.ofHours(1)));
        
        // 用户相关缓存
        cacheConfigurations.put("users", defaultConfig.entryTtl(Duration.ofMinutes(30)));
        cacheConfigurations.put("users_list", defaultConfig.entryTtl(Duration.ofMinutes(15)));
        
        // 系统设置缓存（长期缓存）
        cacheConfigurations.put("system_settings", defaultConfig.entryTtl(Duration.ofHours(24)));
        
        // 权限缓存
        cacheConfigurations.put("permissions", defaultConfig.entryTtl(Duration.ofHours(2)));
        
        // 统计缓存
        cacheConfigurations.put("statistics", defaultConfig.entryTtl(Duration.ofMinutes(30)));
        
        // 通知缓存
        cacheConfigurations.put("notifications", defaultConfig.entryTtl(Duration.ofMinutes(10)));

        return RedisCacheManager.builder(connectionFactory)
                .cacheDefaults(defaultConfig)
                .withInitialCacheConfigurations(cacheConfigurations)
                .build();
    }
}
